package cn.javabb.core.service.biz;

import cn.javabb.common.model.Tree;
import cn.javabb.common.utils.BuildTree;
import cn.javabb.core.annotation.RedisCache;
import cn.javabb.core.base.BaseService;
import cn.javabb.core.constant.SysConstant;
import cn.javabb.core.entity.biz.CatalogDO;
import cn.javabb.core.entity.sys.RolePermissionsDO;
import cn.javabb.core.mapper.biz.CatalogMapper;
import cn.javabb.core.model.StatusEnum;
import cn.javabb.core.service.sys.RolePermissionService;
import cn.javabb.core.service.sys.UserService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 栏目
 * @Author QINB imqinbao@163.com
 * @CreateDate 2018/11/23/023 16:30
 * @Since V1.0
 */
@Service
public class CatalogService extends BaseService<CatalogDO> {
    @Autowired
    private CatalogMapper catalogMapper;
    @Autowired
    RolePermissionService rolePermissionService;
    @Autowired
    UserService userService;
    public List<CatalogDO> treeTable(){
        List<CatalogDO> cList = this.queryAll();
        return cList;

    }

    /**
     * 获取栏目列表，带文章总数和评论总数
     * @param param
     * @return
     */
    public List<CatalogDO> listCatalogs(JSONObject param){
        param.put(this.SQLID, CatalogMapper.class.getName()+".listCatalogs");
        return this.queryListNoPage(param);
    }
    @RedisCache
    public CatalogDO getCatalogById(Integer catalogId){
        JSONObject param = new JSONObject();
        param.put("catalogId",catalogId);
        param.put(this.SQLID, CatalogMapper.class.getName()+".listCatalogs");
        return this.queryOne(param);
    }
    /**
     * 获取Catalog的树列表,默认根栏目为-1
     * @return
     */
    public List<Tree> listTree(){
        List<CatalogDO> cList = this.queryAll();
        List<Tree> tList = new ArrayList<Tree>();
        Tree rootTree = new Tree();
        rootTree.setName("根栏目");
        rootTree.setId("0");
        rootTree.setParentId("-1");
        tList.add(rootTree);
        for(CatalogDO c:cList){
            Tree t = new Tree();
            t.setId(c.getCatalogId()+"");
            t.setName(c.getCatalogName());
            t.setParentId(c.getParentId()+"");
            t.setOpen(true);
            t.setChecked(false);
            tList.add(t);
        }
        return BuildTree.buildList(tList,"-1");
    }

    /**
     * 首页获取
     * @return
     */
    @RedisCache
    public List<CatalogDO> listIndexCatalogs(){
        JSONObject param = new JSONObject();
        param.put("state", StatusEnum.NORMAL.getCode());
        param.put("indexShow",1);
        return getCatalogTree(this.listCatalogs(param),0);
    }


    public List<CatalogDO> getCatalogTree(List<CatalogDO> listCatalogs,Integer parentId){

        List<CatalogDO> newList = new ArrayList<>();
        for(CatalogDO c:listCatalogs){
            if(parentId == c.getParentId()){
                c.setChildren(getCatalogTree(listCatalogs,c.getCatalogId()));
                newList.add(c);
            }
        }
        return newList;
    }

    /**
     * 根据role获取板块的权限
     * @param roleId
     * @return
     */
    public List<Tree> checkTreeByRoleId(Integer roleId){
        List<Tree> treeList = new ArrayList<>();
        //查找Catalog树
        List<CatalogDO> listCatalogs = this.queryAll();
        List<RolePermissionsDO> rpList = rolePermissionService.listByRoleId(roleId, SysConstant.PERMISSION_TYPE_CATALOG);
        listCatalogs.forEach(c -> {
            Tree t = new Tree();
            t.setId(c.getCatalogId().toString());
            t.setParentId(c.getParentId().toString());
            t.setName(c.getCatalogName());
            rpList.forEach(rp -> {
                if(c.getCatalogId()==rp.getPermissionId()){
                    t.setChecked(true);
                }
            });
            treeList.add(t);
        });
        return treeList;
    }

    /**
     * 查询用户的板块权限
     * @param userId
     * @return
     */
    public List<CatalogDO> listCatalogPermissionsByUserId(Integer userId) {
        return catalogMapper.listCatalogPermissionByUserId(userId);
    }
}
